<?php
namespace App\Controllers\Common;


use App\Models\User\User;
use App\Libs\Views\Json;
use App\Libs\Helper\WeixinGzhHelper;
class GetMemberWeixin extends \App\Libs\Base\BaseAction
{


	public function execute(){

        $code = $this->request->getQuery('code');

        $user = $this->session->user;
        $curUser = User::findOne([
            'id=?1',
            'bind' => [1=>$user->id],
            "for_update" => true
        ]);
        if(!empty($curUser->unionid)){
            return Json::view(1,'您已绑定过微信，无需再次绑定',NULL);
        }
        $userInfo = WeixinGzhHelper::getUserinfo($code);
        if(empty($userInfo)){
            return Json::view(1,'绑定失败',NULL);
        }
        $curUser->unionid = $userInfo['unionid'];
        if($curUser->save){
            return Json::view(0,'绑定成功',NULL);
        }else{
            return Json::view(1,'绑定失败，请重试',NULL);
        }
    }


}
